<template>
  <page-view>
    <el-card>
      <template #header>
        <div class="font-bold">用户信息列表</div>
      </template>
      <el-form :model="queryFormData" :inline="true" class="flex ">
        <el-form-item label="用户名">
          <el-input placeholder="请输入用户名" v-model="queryFormData.nickName"></el-input>
        </el-form-item>
        <el-form-item label="用户手机号">
          <el-input placeholder="请输入用户手机号" v-model="queryFormData.user_phone"></el-input>
        </el-form-item>
        <el-form-item label="性别">
          <el-select v-model="value" class="mr-[25px]" placeholder="请选择性别">
            <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value" />
          </el-select>
        </el-form-item>


        <el-form-item>
          <el-button type="primary" @click="queryData" :loading="isLoading">
            <el-icon>
              <Search />
            </el-icon>
            查询
          </el-button>

        </el-form-item>
      </el-form>
      <div v-loading="isLoading">
        <!--要展示数据了-->
        <el-table :data="resultData.listData" border stripe max-height="550">
          <el-table-column label="id" prop="id" width="90" align="center"></el-table-column>
          <el-table-column align="center" label="用户名称" prop="nickName" width="150"></el-table-column>
          <el-table-column label="性别" prop="user_sex" width="120" align="center"></el-table-column>
          <el-table-column label="手机号" prop="user_phone" align="center"></el-table-column>
          <el-table-column label="邮箱" prop="user_email" align="center"></el-table-column>
          <el-table-column label="用户头像" width="120" align="center">
            <template #default="{ row }">
              <el-image :src="baseURL + row.user_photo" class="w-[50px] h-[50px]" fit="cover"
                :preview-src-list="[baseURL + row.user_photo]" :preview-teleported="true" />
            </template>
          </el-table-column>

          <el-table-column label="操作" align="center">
            <template #default="{ row }">
             
              <el-popconfirm title="你确定要删除吗" @confirm="deleteCurrentItem(row.id)">
                <template #reference>
                  <el-button type="danger" size="small">删除</el-button>
                </template>

              </el-popconfirm>

            </template>
          </el-table-column>
        </el-table>
        <!--页码-->
        <div class="flex flex-row justify-between items-center mt-[10px]">
          <div class="text-[14px]">
            当前第{{ queryFormData.pageIndex }}页，共{{
                resultData.pageCount
            }}页，共{{ resultData.totalCount }}条
          </div>
          <el-pagination background layout="prev, pager, next" :total="resultData.totalCount"
            @current-change="currentChange" />
        </div>
      </div>
    </el-card>
  </page-view>
</template>

<script setup>
import { reactive, ref } from "vue";
import { Search, CirclePlus } from "@element-plus/icons-vue";
import API from "@/utils/API/index.js";
import { txtEllipsis } from "@/utils/StringUtils.js";
import { onMounted, inject } from "vue";
import { ElNotification } from "element-plus";

const baseURL = inject("baseURL");
const isLoading = ref(false);

const value = ref('')
const options = [
  {
    value: "男",
    label: "男",
  }, {
    value: "女",
    label: "女",
  },
];


const queryFormData = reactive({
  nickName: "",
  user_phone: "",
  user_sex: value,
  pageIndex: 1,
});

const resultData = reactive({
  listData: [],
  pageCount: 0,
  totalCount: 0,
});

//查询操作
const queryData = () => {
  isLoading.value = true;
  API.userInfo
    .getListByPage(queryFormData)
    .then((result) => {
      console.log(result);
      resultData.listData = result.listData;
      resultData.pageCount = result.pageCount;
      resultData.totalCount = result.totalCount;
    })
    .finally(() => {
      isLoading.value = false;
    });
};

//页码发生变化
const currentChange = (index) => {
  queryFormData.pageIndex = index;
  queryData();
};



//删除操作
const deleteCurrentItem = (id) => {
  console.log(id);
  API.userInfo.deleteById({ id }).then(result => {
    ElNotification({
      type: "success",
      title: "删除成功",
      message: "当前用户信息删除成功"
    })
    //重新渲染页面
    queryData()
  }).catch(error => {
    ElNotification({
      type: "danger",
      title: "删除失败",
      message: "当前用户信息删除失败，请重试或联系管理员"
    })
  })
}

onMounted(() => {
  queryData();
});
</script>

<style scoped>

</style>
